package com.ycs.operationlog.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @program: operation-log
 * @description:
 * @author: ycs
 * @create: 2022-03-15 22:01
 **/
@EnableTransactionManagement
@Configuration
public class DataSourceConfig {


    @Value("${operationLog.source.driverClassName}")
    private String driverClassName;

    @Value("${operationLog.source.url}")
    private String url;

    @Value("${operationLog.source.username}")
    private String username;

    @Value("${operationLog.source.password}")
    private String password;

    private DataSource dataSource;

    private JdbcTemplate jdbcTemplate;

    public DataSource getDataSource() {
        if (dataSource != null) {
            return dataSource;
        }
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        this.dataSource = dataSource;
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public JdbcTemplate getJdbcTemplate() {
        if (jdbcTemplate != null) {
            return jdbcTemplate;
        }
        this.jdbcTemplate = new JdbcTemplate();
        this.jdbcTemplate.setDataSource(getDataSource());

        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


}
